//
//  MineViewController.m
//  B段项目
//
//  Created by ; on 15/9/28.
//  Copyright (c) 2015年 昶博. All rights reserved.
//

#import "MineViewController.h"
#import "PrefixHeader.pch"
#import "FirstViewController.h"
#import "AboutUsViewController.h"
#import "SaveViewController.h"
#import <UIImageView+WebCache.h>
#import <RESideMenu.h>

@interface MineViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) BOOL night;

@end

@implementation MineViewController

- (void)viewWillAppear:(BOOL)animated
{
    SelfNavBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor clearColor];
    [self tableViewSubView];
}

#pragma mark --tableView初始化
- (void)tableViewSubView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, (self.view.frame.size.height - 54 * 5) / 2.0f, ScreenWidth , 55 * 5) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.backgroundView = nil;
    self.tableView.opaque = NO;
    self.tableView.bounces = NO;
    self.tableView.scrollEnabled = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
}
#pragma mark --cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
    
}
#pragma mark --行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 4;
    
}
#pragma mark --cell赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:21];
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的收藏";
        return cell;
    }else if (indexPath.row == 1){
        
        cell.textLabel.text = @"夜间模式";
        return cell;
        
    }else if (indexPath.row == 2){
        cell.textLabel.text = @"清理缓存";
        return cell;
    }
    else
    {
        cell.textLabel.text = @"关于我们";
        return cell;
    }
}

#pragma mark --点击cell方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //跳转我的收藏页面
    if (indexPath.row == 0) {
        UINavigationController *saveNavi = [[UINavigationController alloc] initWithRootViewController:[[SaveViewController alloc] init]];
        [self presentViewController:saveNavi animated:YES completion:^{
            
        }];
        //开启夜间模式
    }else if (indexPath.row == 1){
        self.night = !self.night;
        
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        if (self.night == 1 ) {
            [center postNotificationName:@"changeNight" object:[UIColor blackColor]];
            
        }else{
            [center postNotificationName:@"changeNight" object:[UIColor whiteColor]];
        }
        //清理缓存
    }else if (indexPath.row == 2){
        //清理缓存方法
        [self moveCaches];
        //关于我们
    }else
    {
        UINavigationController *aboutNavi = [[UINavigationController alloc] initWithRootViewController:[[AboutUsViewController alloc] init]];
        [self presentViewController:aboutNavi animated:YES completion:^{
            
        }];
    }
    
}
#pragma mark - 清除缓存方法
- (void)moveCaches
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
    NSString *str = [NSString stringWithFormat:@"缓存已清除%.1fM", [self folderSizeAtPath:path] + [[SDImageCache sharedImageCache] getSize] / (1024 * 1024)];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"清理缓存" message:str delegate:@"确定" cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
    [alert show];
    
    NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:path];
    for (NSString *p in files) {
        NSError *error;
        NSString *Path = [path stringByAppendingPathComponent:p];
        if ([[NSFileManager defaultManager] fileExistsAtPath:Path]) {
            [[NSFileManager defaultManager] removeItemAtPath:Path error:&error];
        }
    }
    [[SDImageCache sharedImageCache] clearDisk];
    [[SDImageCache sharedImageCache] clearMemory];
    
}

- (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    return 0;
}

#pragma mark --遍历文件夹获得文件夹大小，返回多少M
- (float )folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
